package controller

import (
	"Software/global"
	"Software/model"
	"Software/response"
	"Software/utils"
	"context"
	"github.com/gin-gonic/gin"
	"github.com/minio/minio-go/v7"
	"io"
)

// @Summary 上传软件
// @Produce  json
// @Param name query string false "软件名称" maxlength(100)
// @Param info query string false "软件详情" maxlength(100)
// @Param content query string false "破解信息" maxlength(100)
// @Param img formData file false "软件照片"
// @Param apk formData file false "软件安装包"
// @Success 200 {object} model.Response "成功"
// @Failure 400 {object} model.Response "请求错误"
// @Router /software/createSoftware [post]
func CreateSoftware(ctx *gin.Context) {
	img, err := ctx.FormFile("img")
	if err != nil {
		response.Response(ctx, 400, nil, "获取照片失败")
		return
	}

	apk, err := ctx.FormFile("apk")
	if err != nil {
		response.Response(ctx, 400, nil, "安装包获取失败")
		return
	}

	file1, _ := img.Open()
	file2, _ := apk.Open()

	name := ctx.PostForm("name")
	info := ctx.PostForm("info")
	content := ctx.PostForm("content")

	global.DB.Create(&model.Software{
		Name:       name,
		ImagePath:  "img/img-" + name + ".jpg",
		FilePath:   "apk/apk-" + name + ".exe",
		BucketName: "qing",
		Info:       info,
		Content:    content,
	})

	_, err = global.MC.PutObject(context.Background(), "qing", "img/img-"+name+".jpg", file1, -1, minio.PutObjectOptions{})
	if err != nil {
		response.Response(ctx, 408, nil, "照片上传失败")
		return
	}

	_, err = global.MC.PutObject(context.Background(), "qing", "apk/apk-"+name+".exe", file2, -1, minio.PutObjectOptions{})
	if err != nil {
		response.Response(ctx, 408, nil, "软件上传失败")
		return
	}

	response.Response(ctx, 200, nil, "上传成功")
}

// @Summary 下载软件
// @Produce  json
// @Param ID query string false "软件ID" maxlength(100)
// @Success 200 {object} model.Response "成功"
// @Failure 400 {object} model.Response "请求错误"
// @Router /software/downloadSoftware [post]
func DownloadSoftware(ctx *gin.Context) {
	var softwareReq model.SoftwareReq
	err := ctx.ShouldBind(&softwareReq)
	if err != nil {
		response.Response(ctx, 400, nil, "数据绑定失败")
		return
	}

	var sw model.Software
	global.DB.Where("id = ?", softwareReq.ID).Find(&sw)

	response.Response(ctx, 200, gin.H{
		"bucket":       sw.BucketName,
		"downloadPath": sw.FilePath,
	}, "查找成功")
}

// @Summary 获取软件详细信息
// @Produce  json
// @Param ID query string false "软件ID" maxlength(100)
// @Success 200 {object} model.Response "成功"
// @Failure 400 {object} model.Response "请求错误"
// @Router /software/findDetailedSoftware [post]
func FindDetailedSoftware(ctx *gin.Context) {
	var softwareReq model.SoftwareReq
	err := ctx.ShouldBind(&softwareReq)
	if err != nil {
		response.Response(ctx, 400, nil, "数据绑定失败")
		return
	}

	var sw model.Software
	global.DB.Where("id = ?", softwareReq.ID).Find(&sw)

	object, err := global.MC.GetObject(context.Background(), sw.BucketName, sw.ImagePath, minio.GetObjectOptions{})
	if err != nil {
		response.Response(ctx, 400, nil, "获取照片失败")
		return
	}

	data, _ := io.ReadAll(object)
	code := utils.PicToBase64(data)
	response.Response(ctx, 200, gin.H{
		"name":      sw.Name,
		"imgBase64": code,
		"info":      sw.Info,
		"content":   sw.Content,
	}, "查找成功")
}

// @Summary 获取软件清单
// @Produce  json
// @Success 200 {object} model.Response "成功"
// @Failure 400 {object} model.Response "请求错误"
// @Router /software/findSoftwareList [get]
func FindSoftwareList(ctx *gin.Context) {
	var allSoftware []model.Software

	global.DB.Find(&allSoftware)

	var total []model.SoftwareRsp
	for i := 0; i < len(allSoftware); i++ {
		object, err := global.MC.GetObject(context.Background(), allSoftware[i].BucketName, allSoftware[i].ImagePath, minio.GetObjectOptions{})

		if err != nil {
			total = append(total, model.SoftwareRsp{
				Name: allSoftware[i].Name,
				Code: "",
			})
			continue
		}

		data, _ := io.ReadAll(object)
		total = append(total, model.SoftwareRsp{
			ID:   allSoftware[i].ID,
			Name: allSoftware[i].Name,
			Code: utils.PicToBase64(data),
		})
	}

	response.Response(ctx, 200, gin.H{
		"list": total,
	}, "查找成功")
}
